#include <string>
#include <iostream>
#include "HTMLFetcher.h"
#include "RobotVerifier.h"
#include "LinkParser.h"
#include <vector>
#include <sstream>
#include "md5/md5.h"
#include "Domain.h"
#include "Site.h"
#include "Probe.h"
#include "NCManager.h"
#include "QueueManager.h"
#include "Statistics.h"
#include "BackUpSlave.h"
#include <pthread.h>
#include "Phoenix.h"
#include <csignal>
#define NUM_THREADS 800

using namespace std;
  

  
  int main(int argc, char* argv[])
  {
	  
	  
	  signal (SIGPIPE, SIG_IGN);          
	  
	  pthread_attr_t attr;
	  pthread_attr_init(&attr);
	  int stacksize = 600*1024;
	  pthread_attr_setstacksize(&attr,stacksize);
	  
	  pthread_attr_t attr1;
	  pthread_attr_init(&attr1);
	  int stacksize1 = 16000*1024;
	  pthread_attr_setstacksize(&attr1,stacksize1);
	  
	  Statistics::init();
	  NCManager* mothership = new NCManager();
	  mothership->init();
	  Probe::setMotherShip(mothership);
	  for(int i=0;i< argc;i++)
		  cout << "Arg " << i << " vale: " << argv[i] << endl;
	  if(argc < 2)
	  {
	  mothership->addToVisitQueue("http://www.uol.com.br");
	  //mothership->addToVisitQueue("http://www.centraldoslinks.com");
	  mothership->addToVisitQueue("http://www.cade.com.br");
	  mothership->addToVisitQueue("http://www.terra.com.br");
	  mothership->addToVisitQueue("http://www.globo.com");
	  mothership->addToVisitQueue("http://www.yahoo.com.br");
	  mothership->addToVisitQueue("http://www.cnn.com");
	  mothership->addToVisitQueue("http://www.aol.com");
	  mothership->addToVisitQueue("http://www.lemonde.fr");
	  mothership->addToVisitQueue("http://www.google.com");
	  mothership->addToVisitQueue("http://www.geocities.com");
	  mothership->addToVisitQueue("http://www.yahoo.com");
	  mothership->addToVisitQueue("http://www.msn.com");
	  mothership->addToVisitQueue("http://www.ig.com.br");
	 // mothership->addToVisitQueue("http://www.bobooll.com.br");
	  mothership->addToVisitQueue("http://www.orkut.com");
	  mothership->addToVisitQueue("http://www.folha.com.br");
	  //mothership->addToVisitQueue("http://www.ufmg.br");
	  //mothership->addToVisitQueue("http://www.bibl.ita.br");
	  }
	  else
	  {
		  cout << "calling backFromFire()" << endl;
		  Phoenix::backFromFire();
		  cout << "back from backFromFire()"<<endl;
	  }
	  pthread_t threads[NUM_THREADS];
  

	  int l =0; 
//	  QueueManager::setMotherShip(mothership);
//	  QueueManager::setMaxQueueSize(200000);
//	  cout << "tou aki 2" << endl;
	  int queueThreadId = pthread_create(&threads[0], &attr1, BackUpSlave::backUpJob, (void *) l );
	   int rc, t;
	   for(t=1; t<(NUM_THREADS+1); t++){
	      rc = pthread_create(&threads[t], &attr, Probe::run, (void *)t); 
	   } 
/*TODO:
 * join threads
 */
	   while(true)
	   {
		   sleep(100);
			//pthread_mutex_lock(&Statistics::LP_Mutex);
			//pthread_mutex_lock(&Statistics::CC_Mutex);
			//pthread_mutex_lock(&Statistics::HF_Mutex);
			pthread_mutex_lock(&mothership->robotsCounterMutex);
			pthread_mutex_lock(&mothership->pageCounterMutex);
				Statistics::statsReport();
			pthread_mutex_unlock(&mothership->pageCounterMutex);
			pthread_mutex_unlock(&mothership->robotsCounterMutex);
			//pthread_mutex_unlock(&Statistics::HF_Mutex);
			//pthread_mutex_unlock(&Statistics::CC_Mutex);
			//pthread_mutex_unlock(&Statistics::LP_Mutex);
	   }
	   // t=1 por hora
	   void *status;
	   for(t=1; t<NUM_THREADS; t++)
	   {
	      rc = pthread_join(threads[t], &status);
	      if (rc)
	         cout << "ERROR: return code from pthread_join() is " << rc << endl;	         
	      cout << "Completed join with thread " << t << " status= " << (long)status << endl;
	   }	   
	   
	   pthread_exit(NULL); 
  

  }
  
  
  
